﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuickORM.BLL;
using QuickORM.Entity;
using QuickORM.Common;
public partial class Manage_WebPages_UserInfo_UserEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDL(ddlRole);

            T_UserInfo userInfo = new T_UserInfo();
            userInfo = BLLFactory<T_UserInfoManager>.Instance.FindByID(LoginUser.Instance.UserID);
            BindPage(userInfo);

        }
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        string js = "alert('操作失败！');";
        T_UserInfo user = new T_UserInfo();
        user.UserID = int.Parse(LoginUser.Instance.UserID);
        user.UserName = Util.querySQL(txtUserName.Value.Trim());
        user.UserPWD = Util.SHA1(txtUserPWD.Value);
        user.RealName = Util.querySQL(txtRealName.Value.Trim());
        user.Dep = Util.querySQL(txtDep.Value.Trim());
        user.Email = Util.querySQL(txtEmail.Value.Trim());
        user.Tel = Util.querySQL(txtTel.Value.Trim());
        if(!string.IsNullOrEmpty(txtUserPWD.Value))
        {
            user.UserPWD = Util.SHA1(txtUserPWD.Value);
        }

        //修改
        if (BLLFactory<T_UserInfoManager>.Instance.Edit(user)>0)
        {
            js = "window.close();";
        }

        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", js, true);
    }
    #region Fill DroDownList
    protected void BindDDL(DropDownList ddl)
    {
        ddl.DataSource = BLLFactory<T_RoleManager>.Instance.GetAll();
        ddl.DataBind();
    }
    #endregion
    #region 填充页面
    /// <summary>
    /// 将数据填充到页面显示
    /// </summary>
    /// <param name="info">实体类数据</param>
    protected void BindPage(T_UserInfo info)
    {
        txtUserName.Value = info.UserName;
        txtUserPWD.Value = "123456";
        txtRealName.Value = info.RealName;
        txtDep.Value = info.Dep;
        txtEmail.Value = info.Email;
        txtTel.Value = info.Tel;
        ddlRole.SelectedValue = info.RoleID.ToString();
    }
    #endregion
}